#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tools import *
from numpy.random import RandomState
from sklearn.linear_model import LogisticRegression

# 获取训练样本
data = loadData()
X = np.vstack([data[:40, :-1], data[50:90, :-1], data[100:140, :-1]])
y = np.hstack([data[:40, -1], data[50:90, -1], data[100:140, -1]])

# 初始化分类器,最优化算法使用 lbfgs
# https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html
rs = RandomState()
lr = LogisticRegression(
    tol=1e-3, solver='lbfgs', max_iter=1000, random_state=rs)

# 训练样本
lr.fit(X, y)

# 测试
Xt = np.vstack([data[40:50, :-1], data[90:100, :-1], data[140:, :-1]])
yt = np.hstack([data[40:50, -1], data[90:100, -1], data[140:, -1]])
haty = lr.predict(Xt)
print('correct: %.2f%%' % (lr.score(Xt, yt) * 100))

# output: correct: 100.00%
